package com.secondary.action.goodsAction;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.secondary.action.BaseAction;
import com.secondary.pojos.Category;
import com.secondary.pojos.Goods;
import com.secondary.pojos.GoodsPic;
import com.secondary.pojos.User;
import com.secondary.service.CategoryService;
import com.secondary.service.GoodsService;
import com.secondary.util.MyImageUtil;

@ParentPackage(value = "authority")
@Namespace(value = "/saveGoods")
@Action(value = "saveGoods")
@Results( { @Result(name = "saveSuccess", location = "/changeGoods/changeGoods!showUserGoods.action", type = "redirect"),

})
public class SaveGoodsAction extends BaseAction {
	@Resource
	private GoodsService goodsService;
	@Resource
	private CategoryService categoryService;
	private Goods goods;
	private File[] files;
	private String[] filesFileName;
	private String[] filescontentType;

	public String saveGood() {

		User user = (User) super.getSessionAttribute("user");

		goods.setSeller(user);

		Set<Category> categorys = new HashSet<Category>();
		categorys.add((Category) categoryService
				.findParentCategoryObject(Integer.parseInt(super
						.getRequestParameter("category1"))));
		categorys.add((Category) categoryService
				.findParentCategoryObject(Integer.parseInt(super
						.getRequestParameter("category2"))));
		System.out.println("/////////////,.,,,,,,,,,,<><<>>>>>>>>>>>"+categorys.size());
		goods.setCategorys(categorys);
		goodsService.saveGood(goods);
		String path = ServletActionContext.getServletContext().getRealPath(
				"/user_images_" + user.getId());
		MyImageUtil.savePics(files, path, filesFileName);
		if(filesFileName!=null){
		for (int i = 0; i < filesFileName.length; i++) {
			GoodsPic pic = new GoodsPic();
			pic.setPicUrl(filesFileName[i]);
			pic.setGoods(goods);
			goodsService.savePic(pic);
		}
		}
		return "saveSuccess";
	}

	public GoodsService getGoodsService() {
		return goodsService;
	}

	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public File[] getFiles() {
		return files;
	}

	public void setFiles(File[] files) {
		this.files = files;
	}

	public String[] getFilesFileName() {
		return filesFileName;
	}

	public void setFilesFileName(String[] filesFileName) {
		this.filesFileName = filesFileName;
	}

	public String[] getFilescontentType() {
		return filescontentType;
	}

	public void setFilescontentType(String[] filescontentType) {
		this.filescontentType = filescontentType;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

}
